import 'dart:io';

import 'package:sl_doclever/sl_doclever.dart';
import 'package:sl_fast_dev/src/sl_fast_dev_config.dart';
import 'package:sl_fast_dev/src/template/sl_templete.dart';

class SLDocleverRAC {
  RACInfo racInfo;

  String rac_mtd;
  String rac_mtd_invoke;
  String rac_properties;

  SLDocleverRAC(String rootName, List<String> pathes) {
    // configration
    SLDocConfig().prefix = SLFastDevConfig().prefix;
    SLDocConfig().root = rootName;

    // map to rac model info
    var doclever = SLDoclever(pathes);
    racInfo = SLObjcHttp(doclever.docDatas).rac_all_request();

    // load templete file.
    var f = File(sl_doc_mtd).readAsStringSync();

    // replace keywords
    var mtdL = [], mtdUse = [], cmdL = [];
    racInfo.mtdL.forEach((http) {
      var r = f.replaceAll('[DOCLEVER_M_DESC]', http.desc);
      r = r.replaceAll('[DOCLEVER_OBJC]', http.cmd);
      r = r.replaceAll('[DOCLEVER_M_OBJ]', racInfo.name);
      r = r.replaceAll('[DOCLEVER_MTD]', http.mtd);

      mtdL.add(r);
      mtdUse.add('[self seg_${http.cmd}];');
      cmdL.add('@property (nonatomic, strong) RACCommand *${http.cmd};');
    });

    // data bind
    rac_mtd = mtdL.join('\n');
    rac_mtd_invoke = mtdUse.join('\n');
    rac_properties = cmdL.join('\n');
  }

  @override
  String toString() => 'rac_mtd:$rac_mtd,'
      'rac_mtd_invoke:$rac_mtd_invoke,'
      'rac_properties:$rac_properties\n';
}
